package com.business;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.bean.ChatBean;
import com.bean.SessionBean;
import com.bean.TwoTuple;
import com.service.FeedbackService;

public class ChatBusiness {
	private FeedbackService feedbackService;
	
	public String getPublicSession(int offset, int rows){
		String json_str = "";
		TwoTuple<Integer, List<SessionBean>> tuple = feedbackService.getPublicSession(offset, rows);
		int total = tuple.first;
		List<SessionBean> session_lst = tuple.second;
		JSONObject json = new JSONObject();
		JSONArray jarray;
		json.put("total", total);
		if(total == 0){
			return json.toString();
		}
		if(session_lst.size() == 0){
			return json.toString();
		}
		if(session_lst.size()>0){
			jarray = JSONArray.fromObject(session_lst);
			json.put("session_lst", jarray);
		}
		json_str = json.toString();
		return json_str;
	}
	
	public String getSessionByStaff(String ename, int offset, int rows){
		String json_str = "";
		TwoTuple<Integer, List<SessionBean>> tuple = feedbackService.getSessionByStaff(ename, offset, rows);
		int total = tuple.first;
		List<SessionBean> session_lst = tuple.second;
		JSONObject json = new JSONObject();
		JSONArray jarray;
		json.put("total", total);
		if(total == 0){
			return json.toString();
		}
		if(session_lst.size() == 0){
			return json.toString();
		}
		if(session_lst.size()>0){
			jarray = JSONArray.fromObject(session_lst);
			json.put("session_lst", jarray);
		}
		json_str = json.toString();
		return json_str;
	}
	
	public String getSessionByDirector(int offset, int rows){
		String json_str = "";
		TwoTuple<Integer, List<SessionBean>> tuple = feedbackService.getSessionByDirector(offset, rows);
		int total = tuple.first;
		List<SessionBean> session_lst = tuple.second;
		JSONObject json = new JSONObject();
		JSONArray jarray;
		json.put("total", total);
		if(total == 0){
			return json.toString();
		}
		if(session_lst.size() == 0){
			return json.toString();
		}
		if(session_lst.size()>0){
			jarray = JSONArray.fromObject(session_lst);
			json.put("session_lst", jarray);
		}
		json_str = json.toString();
		return json_str;
	}
	
	public String queryChatDetail(int sessionid, int role){
		String json_str = "";
		List<ChatBean> chat_lst = feedbackService.inquireChatDetail(sessionid, role);
		JSONArray jarray;
		if(chat_lst.size()>0){
			jarray = JSONArray.fromObject(chat_lst);
			return jarray.toString();
		}
		return json_str;
		
	}
	
	public FeedbackService getFeedbackService() {
		return feedbackService;
	}

	public void setFeedbackService(FeedbackService feedbackService) {
		this.feedbackService = feedbackService;
	}
	
	

}
